package com.imooc.controller;

import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.vo.CommentLevelCountsVO;
import com.imooc.pojo.vo.ItemInfoVO;
import com.imooc.pojo.vo.ShopcartVO;
import com.imooc.service.CarouselService;
import com.imooc.service.CategoryService;
import com.imooc.service.ItemService;
import imooc.utils.ApiResponse;
import imooc.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController extends BaseController{

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ItemService itemService;


    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/26 09：14
     * @description    查询商品详情
     **/
    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public ApiResponse info(
            @ApiParam(name = "itemId", value = "商品ID",required = true)
            @PathVariable  String itemId) throws Exception {
        if (StringUtils.isBlank(itemId)) {
            return ApiResponse.errorMsg("");
        }

        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemParams(itemsParam);
        itemInfoVO.setItemSpecList(itemsSpecList);
        return ApiResponse.ok(itemInfoVO);
    }



    @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public ApiResponse commentLevel(
            @ApiParam(name = "itemId", value = "商品ID",required = true)
            @RequestParam String itemId) throws Exception {
        if (StringUtils.isBlank(itemId)) {
            return ApiResponse.errorMsg("");
        }
        CommentLevelCountsVO commentLevelCountsVO = itemService.queryCommentCounts(itemId);
        return ApiResponse.ok(commentLevelCountsVO);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/27 10:30
     * @description    查询商品评论
     **/
    @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public ApiResponse comments(
            @ApiParam(name = "itemId", value = "商品ID",required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评论等级",required = true)
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "第几页",required = true)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页数量",required = true)
            @RequestParam Integer pageSize) throws Exception {
        if (StringUtils.isBlank(itemId)) {
            return ApiResponse.errorMsg("");
        }
        if (null == page) {
            page = COMMENT_PAGE;
        }
        if (null == pageSize) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult result = itemService.queryPagedComment(itemId, level, page, pageSize);
        return ApiResponse.ok(result);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/27 14:44
     * @description    搜索商品
     **/
    @ApiOperation(value = "搜索商品", notes = "搜索商品", httpMethod = "GET")
    @GetMapping("/search")
    public ApiResponse search(
            @ApiParam(name = "keywords", value = "搜索的关键词",required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序的条件",required = true)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "第几页",required = true)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页数量",required = true)
            @RequestParam Integer pageSize) throws Exception {
        if (StringUtils.isBlank(keywords)) {
            return ApiResponse.errorMsg("");
        }
        if (null == page) {
            page = COMMENT_PAGE;
        }
        if (null == pageSize) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult result = itemService.searchItems(keywords, sort, page, pageSize);
        return ApiResponse.ok(result);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 8:56
     * @description    根据分类ID，搜索商品
     **/
    @ApiOperation(value = "根据分类ID，搜索商品", notes = "根据分类ID，搜索商品", httpMethod = "GET")
    @GetMapping("/catItems")
    public ApiResponse catItems(
            @ApiParam(name = "catId", value = "分类ID",required = true)
            @RequestParam String catId,
            @ApiParam(name = "sort", value = "排序的条件",required = true)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "第几页",required = true)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页数量",required = true)
            @RequestParam Integer pageSize) throws Exception {
        if (StringUtils.isBlank(catId)) {
            return ApiResponse.errorMsg("");
        }
        if (null == page) {
            page = COMMENT_PAGE;
        }
        if (null == pageSize) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult result = itemService.searchItemsByThirdCat(catId, sort, page, pageSize);
        return ApiResponse.ok(result);
    }



    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 11:37
     * @description    用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格）
     **/
    @ApiOperation(value = "根据商品规格IDS查找最新的商品数据", notes = "根据商品规格IDS查找最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public ApiResponse refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接的规格ids",required = true,example = "1,2,3")
            @RequestParam String itemSpecIds) throws Exception {
        if (StringUtils.isBlank(itemSpecIds)) {
            return ApiResponse.errorMsg("");
        }
        List<ShopcartVO>  shopcartVOList = itemService.queryItemsBySpecIds(itemSpecIds);
        return ApiResponse.ok(shopcartVOList);
    }


}
